//Not needed other for testing/development purposes

class CostCategoryController extends BaseController{
    def beforeInterceptor = [action:this.highestAuth]
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ costCategoryInstanceList: CostCategory.list( params ), costCategoryInstanceTotal: CostCategory.count() ]
    }

    def show = {
        def costCategoryInstance = CostCategory.get( params.id )

        if(!costCategoryInstance) {
            flash.message = "CostCategory not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ costCategoryInstance : costCategoryInstance ] }
    }

    def delete = {
        def costCategoryInstance = CostCategory.get( params.id )
        if(costCategoryInstance) {
            try {
                costCategoryInstance.delete(flush:true)
                flash.message = "CostCategory ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "CostCategory ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "CostCategory not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def costCategoryInstance = CostCategory.get( params.id )

        if(!costCategoryInstance) {
            flash.message = "CostCategory not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ costCategoryInstance : costCategoryInstance ]
        }
    }

    def update = {
        def costCategoryInstance = CostCategory.get( params.id )
        if(costCategoryInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(costCategoryInstance.version > version) {
                    
                    costCategoryInstance.errors.rejectValue("version", "costCategory.optimistic.locking.failure", "Another user has updated this CostCategory while you were editing.")
                    render(view:'edit',model:[costCategoryInstance:costCategoryInstance])
                    return
                }
            }
            costCategoryInstance.properties = params
            if(!costCategoryInstance.hasErrors() && costCategoryInstance.save()) {
                flash.message = "CostCategory ${params.id} updated"
                redirect(action:show,id:costCategoryInstance.id)
            }
            else {
                render(view:'edit',model:[costCategoryInstance:costCategoryInstance])
            }
        }
        else {
            flash.message = "CostCategory not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def costCategoryInstance = new CostCategory()
        costCategoryInstance.properties = params
        return ['costCategoryInstance':costCategoryInstance]
    }

    def save = {
        def costCategoryInstance = new CostCategory(params)
        if(!costCategoryInstance.hasErrors() && costCategoryInstance.save()) {
            flash.message = "CostCategory ${costCategoryInstance.id} created"
            redirect(action:show,id:costCategoryInstance.id)
        }
        else {
            render(view:'create',model:[costCategoryInstance:costCategoryInstance])
        }
    }
}
